<?php
class Teacher_User extends grocery_CRUD_Model
{
    function get_list()
    {
    	if($this->table_name === null)
    		return false;
    	
    	$select = "{$this->table_name}.*";
    	
		// ADD YOUR SELECT FROM JOIN HERE <------------------------------------------------------
		// for example $select .= ", user_log.created_date, user_log.update_date";
		$select .= ", users.username, users.password";
		
    	if(!empty($this->relation))
    		foreach($this->relation as $relation)
    		{
    			list($field_name , $related_table , $related_field_title) = $relation;
    			$unique_join_name = $this->_unique_join_name($field_name);
    			$unique_field_name = $this->_unique_field_name($field_name);
    			
				if(strstr($related_field_title,'{'))
    				$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
    			else    			
    				$select .= ", $unique_join_name.$related_field_title as $unique_field_name";
    			
    			if($this->field_exists($related_field_title))
    				$select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
    		}
    		
    	$this->db->select($select, false);
    	
		// ADD YOUR JOIN HERE for example: <------------------------------------------------------
		// $this->db->join('user_log','user_log.user_id = users.id');
		$this->db->join('users','users.id = '. $this->table_name . '.user_id');
		
    	$results = $this->db->get($this->table_name)->result();
    	return $results;
    }
	
	function get_edit_values($primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
    	$this->db->where($this->table_name . '.' . $primary_key_field,$primary_key_value);
		$this->db->join('users','users.id = ' . $this->table_name . '.user_id');
    	$result = $this->db->get($this->table_name)->row();
		$result->password = '';
    	return $result;
    }

	function db_update($post_array, $primary_key_value)
    {
		$data['nip'] = $post_array["nip"];
		$data['name'] = $post_array["name"];
		$data['address'] = $post_array["address"];
		$data['phone'] = $post_array["phone"];
		$data['city'] = $post_array["city"];
		
    	$primary_key_field = $this->get_primary_key();
		$this->db->update($this->table_name, $data, array($primary_key_field => $primary_key_value));
		
		$rs = $this->get($primary_key_value);
		$user_id = $rs['user_id'];
		$data_user['username'] = $post_array["username"];
		if(!empty($post_array['password']))
			$data_user['password'] = md5($post_array['password']);
			
		$this->db->update('users', $data_user, array('id' => $user_id));
		//echo $this->db->last_query();
		//echo var_dump($data_user) . " data user <Br>" . $user_id . " user end";
    }

	public function get($id) {
		$this->db->where('id',$id);
		$rs = $this->db->get($this->table_name);
		$rs = $rs->result_array();
		return $rs[0];
	}
	
	function db_insert($post_array)
    {
		$CI =& get_instance();
		$CI->load->model('actors_model');
		$rs = $CI->actors_model->get_by_name('teacher');
		
		$datauser['username'] = $post_array['username'];
		$datauser['password'] = md5($post_array['password']);
		$datauser['actor_id'] = $rs['id'];
		$insert = $this->db->insert('users',$datauser);
		$user_id = '';
    	if($insert)
    	{
    		$user_id = $this->db->insert_id();
    	}
		
		$data['nip'] = $post_array["nip"];
		$data['name'] = $post_array["name"];
		$data['address'] = $post_array["address"];
		$data['phone'] = $post_array["phone"];
		$data['city'] = $post_array["city"];
		$data['user_id'] = $user_id;
		
    	$insert = $this->db->insert($this->table_name,$data);
    	if($insert)
    	{
    		return $this->db->insert_id();
    	}
    	return false;
    }
	
	function db_delete($primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
    	
    	if($primary_key_field === false)
    		return false;
		
		$retval_user=false;
		$retval_teacher=false;
		
		$rs = $this->get($primary_key_value);
		$userid = $rs['user_id'];
		$this->db->delete('users', array('id'=>$userid));
		if( $this->db->affected_rows() == 1)
			$retval_user = true;
		
    	$this->db->limit(1);
    	$this->db->delete($this->table_name,array( $primary_key_field => $primary_key_value));
    	if( $this->db->affected_rows() == 1)
    		$retval_teacher= true;
	
		if($retval_teacher==true && $retval_user==true)
			return true;
		else
			return false;
    }
	
}